﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using JY.Common.Extensions;
using JY.Wpf.Core.ViewModelBases;
using JY.Wpf.Test.Views.ControlDemoViews;
using SD.IOC.Core.Mediators;

namespace JY.Wpf.Test.ViewModels.ControlDemoViewModels
{
    /// <summary>
    /// 变更皮肤样式ViewModel
    /// </summary>
    public class ChangeSkinViewModel : ViewModelBase
    {
        #region # 变量

        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public ChangeSkinViewModel()
        {
        }
        #endregion

        #region # 属性
        #endregion

        #region # Command方法

        #region # 默认皮肤样式——void DefaultSkin()
        /// <summary>
        /// 默认皮肤样式
        /// </summary>
        public void DefaultSkin()
        {
            var app = (App)(Application.Current);
            var mergedDictionaries = app.Resources.MergedDictionaries;
            foreach (var resourceDictionary in mergedDictionaries)
            {
                bool isColorDictionary = resourceDictionary.MergedDictionaries.Any(x => x.Source != null && x.Source.ToString().Contains("ColorsDark.xaml"));
                bool isStyleDictionary = resourceDictionary.MergedDictionaries.Any(x => x.Source != null && x.Source.ToString().Contains("HandyControlStyle.xaml"));
                if (isColorDictionary)
                {
                    resourceDictionary.MergedDictionaries.Clear();
                    resourceDictionary.MergedDictionaries.Add(new ResourceDictionary
                    {
                        Source = new Uri("pack://application:,,,/JY.Wpf.Controls;component/Styles/HandyControlStyles/Colors.xaml")
                    });
                }
                else if (isStyleDictionary)
                {
                    List<Uri> oldSources = new List<Uri>();
                    foreach (var oldResourceDictionary in resourceDictionary.MergedDictionaries)
                    {
                        oldSources.Add(oldResourceDictionary.Source);
                    }
                    resourceDictionary.MergedDictionaries.Clear();
                    foreach (var oldSource in oldSources)
                    {
                        resourceDictionary.MergedDictionaries.Add(new ResourceDictionary()
                        {
                            Source = oldSource
                        });
                    }
                }
            }
            Application.Current.MainWindow?.OnApplyTemplate();
        }
        #endregion

        #region # 暗色皮肤样式——void DarkSkin()
        /// <summary>
        /// 暗色皮肤样式
        /// </summary>
        public void DarkSkin()
        {
            var app = (App)(Application.Current);
            var mergedDictionaries = app.Resources.MergedDictionaries;
            foreach (var resourceDictionary in mergedDictionaries)
            {
                bool isColorDictionary=resourceDictionary.MergedDictionaries.Any(x =>x.Source != null && x.Source.ToString().Contains("Colors.xaml"));
                bool isStyleDictionary = resourceDictionary.MergedDictionaries.Any(x => x.Source != null && x.Source.ToString().Contains("HandyControlStyle.xaml"));
                if (isColorDictionary)
                {
                    resourceDictionary.MergedDictionaries.Clear();
                    resourceDictionary.MergedDictionaries.Add(new ResourceDictionary
                    {
                        Source = new Uri("pack://application:,,,/JY.Wpf.Controls;component/Styles/HandyControlStyles/ColorsDark.xaml")
                    });
                }
                else if (isStyleDictionary)
                {
                    List<Uri> oldSources=new List<Uri>();
                    foreach (var oldResourceDictionary in resourceDictionary.MergedDictionaries)
                    {
                        oldSources.Add(oldResourceDictionary.Source);
                    }
                    resourceDictionary.MergedDictionaries.Clear();
                    foreach (var oldSource in oldSources)
                    {
                        resourceDictionary.MergedDictionaries.Add(new ResourceDictionary()
                        {
                            Source = oldSource
                        });
                    }
                }
            }
            Application.Current.MainWindow?.OnApplyTemplate();
        }
        #endregion
        #endregion

        #region # 方法
        #endregion

        #region # 获取页面 —— object GetView(object context = null)
        /// <summary>
        /// 获取页面
        /// </summary>
        /// <param name="context">页面</param>
        /// <returns></returns>
        public override object GetView(object context = null)
        {
            return ResolveMediator.Resolve<ChangeSkinView>();
        }
        #endregion
    }
}
